{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# https://scikit-learn.org/stable/modules/naive_bayes.html#gaussian-naive-bayes\n",
    "# https://scikit-learn.org/stable/modules/tree.html#classification\n",
    "# https://scikit-learn.org/stable/modules/sgd.html#classification\n",
    "# https://scikit-learn.org/stable/modules/svm.html#classification\n",
    "# https://scikit-learn.org/stable/modules/generated/sklearn.ensemble.ExtraTreesClassifier.html#sklearn.ensemble.ExtraTreesClassifier"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "ename": "ModuleNotFoundError",
     "evalue": "No module named 'matplotlib'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mModuleNotFoundError\u001b[0m                       Traceback (most recent call last)",
      "\u001b[1;32m~\\AppData\\Local\\Temp\\ipykernel_23856\\3185057536.py\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m     10\u001b[0m \u001b[1;32mimport\u001b[0m \u001b[0mtime\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     11\u001b[0m \u001b[1;32mimport\u001b[0m \u001b[0mrandom\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 12\u001b[1;33m \u001b[1;32mimport\u001b[0m \u001b[0mmatplotlib\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mpyplot\u001b[0m \u001b[1;32mas\u001b[0m \u001b[0mplt\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m     13\u001b[0m \u001b[1;32mfrom\u001b[0m \u001b[0mscipy\u001b[0m \u001b[1;32mimport\u001b[0m \u001b[0minterp\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     14\u001b[0m \u001b[1;32mimport\u001b[0m \u001b[0mwarnings\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mModuleNotFoundError\u001b[0m: No module named 'matplotlib'"
     ]
    }
   ],
   "source": [
    "import math\n",
    "from sklearn.model_selection import KFold\n",
    "from sklearn import metrics\n",
    "from sklearn.metrics import precision_recall_curve\n",
    "from sklearn import preprocessing\n",
    "\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "\n",
    "import time\n",
    "import random\n",
    "import matplotlib.pyplot as plt\n",
    "from scipy import interp\n",
    "import warnings\n",
    "warnings.filterwarnings(\"ignore\")\n",
    "\n",
    "from sklearn.ensemble import ExtraTreesClassifier\n",
    "from sklearn.naive_bayes import GaussianNB\n",
    "from sklearn.tree import DecisionTreeClassifier\n",
    "from sklearn.linear_model import SGDClassifier\n",
    "from sklearn import svm\n",
    "from collections import Counter\n",
    "from tqdm import tqdm"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.metrics import confusion_matrix\n",
    "from sklearn.metrics import roc_auc_score, auc\n",
    "from sklearn.metrics import precision_recall_fscore_support\n",
    "from sklearn.metrics import precision_recall_curve\n",
    "from sklearn.metrics import classification_report"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def load_data(directory):\n",
    "\n",
    "    D_SSM = np.loadtxt(directory + '/D_SM.txt')\n",
    "    M_FSM = np.loadtxt(directory + '/M_SM.txt')\n",
    "    print(\"D_SSM\",D_SSM.shape)\n",
    "    print(\"M_FSM\",M_FSM.shape)\n",
    "\n",
    "    #D_SSM = np.loadtxt(directory + '/D_SM.txt')\n",
    "\n",
    "\n",
    "\n",
    "    ID = np.zeros(shape=(D_SSM.shape[0], D_SSM.shape[1]))\n",
    "    IM = np.zeros(shape=(M_FSM.shape[0], M_FSM.shape[1]))\n",
    "\n",
    "    for i in range(D_SSM.shape[0]):\n",
    "        for j in range(D_SSM.shape[1]):\n",
    "            if D_SSM[i][j] == 0:\n",
    "                ID[i][j] = D_SSM[i][j]######D_GSM[i][j]\n",
    "            else:\n",
    "                ID[i][j] = D_SSM[i][j]\n",
    "    for i in range(M_FSM.shape[0]):\n",
    "        for j in range(M_FSM.shape[1]):\n",
    "            if M_FSM[i][j] == 0:\n",
    "                IM[i][j] = M_FSM[i][j]#######M_GSM[i][j]\n",
    "            else:\n",
    "                IM[i][j] = M_FSM[i][j]\n",
    "                \n",
    "    ID = pd.DataFrame(ID).reset_index()\n",
    "    IM = pd.DataFrame(IM).reset_index()\n",
    "    ID.rename(columns = {'index':'id'}, inplace = True)\n",
    "    IM.rename(columns = {'index':'id'}, inplace = True)\n",
    "    ID['id'] = ID['id'] + 1\n",
    "    IM['id'] = IM['id'] + 1\n",
    "    \n",
    "    return ID, IM\n",
    "\n",
    "def sample(directory, random_seed):\n",
    "    all_associations = pd.read_csv(directory + '/drug_mutation_pairs.csv', names=['Drug', 'Mutation', 'label'])\n",
    "    known_associations = all_associations.loc[all_associations['label'] == 1]\n",
    "    known_associations_resistance=all_associations.loc[all_associations['label'] == -1]\n",
    "    unknown_associations = all_associations.loc[all_associations['label'] == 0]\n",
    "    #random_negative = unknown_associations.sample(n=known_associations.shape[0], random_state=random_seed, axis=0)\n",
    "    random_negative = unknown_associations.sample(n=known_associations.shape[0]+known_associations_resistance.shape[0], random_state=random_seed, axis=0)\n",
    "\n",
    "    sample_df = known_associations.append(random_negative)\n",
    "    sample_df.reset_index(drop=True, inplace=True)\n",
    "\n",
    "    return sample_df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def performances(y_true, y_pred, y_prob):\n",
    "\n",
    "    print('y_true:',y_true)\n",
    "    print('y_pred:',y_pred)\n",
    "    #tn, fp, fn, tp = confusion_matrix(y_true, y_pred, labels = [0, 1]).ravel().tolist()\n",
    "    tn, fp, fn, tp = confusion_matrix(y_true, y_pred).ravel().tolist()\n",
    "\n",
    "    pos_acc = tp / sum(y_true)\n",
    "    neg_acc = tn / (len(y_pred) - sum(y_pred)) # [y_true=0 & y_pred=0] / y_pred=0\n",
    "    accuracy = (tp+tn)/(tn+fp+fn+tp)\n",
    "    \n",
    "    recall = tp / (tp+fn)\n",
    "    precision = tp / (tp+fp)\n",
    "    f1 = 2*precision*recall / (precision+recall)\n",
    "    \n",
    "    roc_auc = roc_auc_score(y_true, y_prob)\n",
    "    prec, reca, _ = precision_recall_curve(y_true, y_prob)\n",
    "    aupr = auc(reca, prec)\n",
    "    \n",
    "    print('tn = {}, fp = {}, fn = {}, tp = {}'.format(tn, fp, fn, tp))\n",
    "    print('y_pred: -1 = {} |0 = {} | 1 = {}'.format(Counter(y_pred)[0], Counter(y_pred)[1],Counter(y_pred)[2]))###\n",
    "    print('y_true: -1 = {} | 0 = {} | 1 = {}'.format(Counter(y_true)[0], Counter(y_true)[1], Counter(y_true)[2]))###\n",
    "    print('acc={:.4f}|precision={:.4f}|recall={:.4f}|f1={:.4f}|auc={:.4f}|aupr={:.4f}|pos_acc={:.4f}|neg_acc={:.4f}'.format(accuracy, precision, recall, f1, roc_auc, aupr, pos_acc, neg_acc))\n",
    "    return (y_true, y_pred, y_prob), (accuracy, precision, recall, f1, roc_auc, aupr, pos_acc, neg_acc)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def obtain_data(directory, isbalance):\n",
    "\n",
    "    ID, IM = load_data(directory)\n",
    "\n",
    "    if isbalance:\n",
    "        dtp = sample(directory, random_seed = 1234)\n",
    "    else:\n",
    "        dtp = pd.read_csv(directory + '/drug_mutation_pairs.csv', names=['Drug', 'Mutation', 'label'])\n",
    "\n",
    "    mirna_ids = list(set(dtp['Drug']))\n",
    "    disease_ids = list(set(dtp['Mutation']))\n",
    "    random.shuffle(mirna_ids)\n",
    "    random.shuffle(disease_ids)\n",
    "    print('# Drug = {} | Mutation = {}'.format(len(mirna_ids), len(disease_ids)))\n",
    "\n",
    "    mirna_test_num = int(len(mirna_ids) / 5)\n",
    "    disease_test_num = int(len(disease_ids) / 5)\n",
    "    print('# Test: Drug = {} | Mutation = {}'.format(mirna_test_num, disease_test_num))  \n",
    "    print(dtp)\n",
    "    print(\"ID\",ID)\n",
    "    print(\"IM\",IM)\n",
    "    print(dtp.shape)\n",
    "    print(\"ID\",ID.shape)\n",
    "    print(\"IM\",IM.shape)    \n",
    "    \n",
    "    samples = pd.merge(pd.merge(dtp, ID, left_on = 'Drug', right_on = 'id'), IM, left_on = 'Mutation', right_on = 'id')\n",
    "    samples.drop(labels = ['id_x', 'id_y'], axis = 1, inplace = True)\n",
    "    \n",
    "    return ID, IM, dtp, mirna_ids, disease_ids, mirna_test_num, disease_test_num, samples"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def generate_task_Tp_train_test_idx(samples):\n",
    "    kf = KFold(n_splits = 5, shuffle = True, random_state = 1234)\n",
    "\n",
    "    train_index_all, test_index_all, n = [], [], 0\n",
    "    train_id_all, test_id_all = [], []\n",
    "    fold = 0\n",
    "    for train_idx, test_idx in tqdm(kf.split(samples.iloc[:, 3:])): #train_index与test_index为下标\n",
    "        print('-------Fold ', fold)\n",
    "        train_index_all.append(train_idx) \n",
    "        test_index_all.append(test_idx)\n",
    "\n",
    "        train_id_all.append(np.array(dtp.iloc[train_idx][['Drug', 'Mutation']]))\n",
    "        test_id_all.append(np.array(dtp.iloc[test_idx][['Drug', 'Mutation']]))\n",
    "\n",
    "        print('# Pairs: Train = {} | Test = {}'.format(len(train_idx), len(test_idx)))\n",
    "        fold += 1\n",
    "    return train_index_all, test_index_all, train_id_all, test_id_all"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def generate_task_Tm_Td_train_test_idx(item, ids, dtp):\n",
    "    \n",
    "    test_num = int(len(ids) / 5)\n",
    "    \n",
    "    train_index_all, test_index_all = [], []\n",
    "    train_id_all, test_id_all = [], []\n",
    "    \n",
    "    for fold in range(5):\n",
    "        print('-------Fold ', fold)\n",
    "        if fold != 4:\n",
    "            test_ids = ids[fold * test_num : (fold + 1) * test_num]\n",
    "        else:\n",
    "            test_ids = ids[fold * test_num :]\n",
    "\n",
    "        train_ids = list(set(ids) ^ set(test_ids))\n",
    "        print('# {}: Train = {} | Test = {}'.format(item, len(train_ids), len(test_ids)))\n",
    "\n",
    "        test_idx = dtp[dtp[item].isin(test_ids)].index.tolist()\n",
    "        train_idx = dtp[dtp[item].isin(train_ids)].index.tolist()\n",
    "        random.shuffle(test_idx)\n",
    "        random.shuffle(train_idx)\n",
    "        print('# Pairs: Train = {} | Test = {}'.format(len(train_idx), len(test_idx)))\n",
    "        assert len(train_idx) + len(test_idx) == len(dtp)\n",
    "\n",
    "        train_index_all.append(train_idx) \n",
    "        test_index_all.append(test_idx)\n",
    "        \n",
    "        train_id_all.append(train_ids)\n",
    "        test_id_all.append(test_ids)\n",
    "        \n",
    "    return train_index_all, test_index_all, train_id_all, test_id_all"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "def run_rf(train_index_all, test_index_all, samples, classfier):\n",
    "    \n",
    "    fold = 0\n",
    "    for train_idx, test_idx in zip(train_index_all, test_index_all):\n",
    "        print('-----------------------Fold = ', str(fold))\n",
    "\n",
    "        X = samples.iloc[:, 3:]\n",
    "        y = samples['label']\n",
    "\n",
    "        scaler = preprocessing.MinMaxScaler().fit(X.iloc[train_idx,:])\n",
    "        X = scaler.transform(X)\n",
    "\n",
    "        x_train, y_train = X[train_idx], y[train_idx]\n",
    "        x_test, y_test = X[test_idx], y[test_idx]\n",
    "\n",
    "        if classfier == 'ERT':\n",
    "            clf = ExtraTreesClassifier(random_state = 19961231)\n",
    "        elif classfier == 'GNB':\n",
    "            clf = GaussianNB()\n",
    "        elif classfier == 'DT':\n",
    "            clf = DecisionTreeClassifier(random_state = 19961231)\n",
    "        elif classfier == 'SGD':\n",
    "            clf = SGDClassifier(loss=\"hinge\", penalty=\"l2\", max_iter=5)\n",
    "        elif classfier == 'SVM':\n",
    "            clf = svm.SVC()\n",
    "            \n",
    "        clf.fit(x_train, y_train)\n",
    "\n",
    "        y_train_prob = clf.predict_proba(x_train)\n",
    "        y_test_prob = clf.predict_proba(x_test)\n",
    "\n",
    "        y_train_pred = clf.predict(x_train)\n",
    "        y_test_pred = clf.predict(x_test)\n",
    "\n",
    "        print('Train:')\n",
    "        ys_train, metrics_train = performances(y_train, y_train_pred, y_train_prob[:, 1])\n",
    "        print('Test:')\n",
    "        ys_test, metrics_test = performances(y_test, y_test_pred, y_test_prob[:, 1])\n",
    "\n",
    "        fold += 1\n",
    "    \n",
    "    return ys_train, metrics_train, ys_test, metrics_test"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "5it [00:00, 501.32it/s]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "# Drug = 184 | Mutation = 603\n",
      "# Test: Drug = 36 | Mutation = 120\n",
      "========== isbalance = True | task = Tp\n",
      "-------Fold  0\n",
      "# Pairs: Train = 1470 | Test = 368\n",
      "-------Fold  1\n",
      "# Pairs: Train = 1470 | Test = 368\n",
      "-------Fold  2\n",
      "# Pairs: Train = 1470 | Test = 368\n",
      "-------Fold  3\n",
      "# Pairs: Train = 1471 | Test = 367\n",
      "-------Fold  4\n",
      "# Pairs: Train = 1471 | Test = 367\n",
      "-----------------------Fold =  0\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Train:\n",
      "tn = 750, fp = 0, fn = 0, tp = 720\n",
      "y_pred: 0 = 750 | 1 = 720\n",
      "y_true: 0 = 750 | 1 = 720\n",
      "acc=1.0000|precision=1.0000|recall=1.0000|f1=1.0000|auc=1.0000|aupr=1.0000|pos_acc=1.0000|neg_acc=1.0000\n",
      "Test:\n",
      "tn = 119, fp = 50, fn = 51, tp = 148\n",
      "y_pred: 0 = 170 | 1 = 198\n",
      "y_true: 0 = 169 | 1 = 199\n",
      "acc=0.7255|precision=0.7475|recall=0.7437|f1=0.7456|auc=0.7239|aupr=0.8149|pos_acc=0.7437|neg_acc=0.7000\n",
      "-----------------------Fold =  1\n",
      "Train:\n",
      "tn = 726, fp = 0, fn = 0, tp = 744\n",
      "y_pred: 0 = 726 | 1 = 744\n",
      "y_true: 0 = 726 | 1 = 744\n",
      "acc=1.0000|precision=1.0000|recall=1.0000|f1=1.0000|auc=1.0000|aupr=1.0000|pos_acc=1.0000|neg_acc=1.0000\n",
      "Test:\n",
      "tn = 139, fp = 54, fn = 36, tp = 139\n",
      "y_pred: 0 = 175 | 1 = 193\n",
      "y_true: 0 = 193 | 1 = 175\n",
      "acc=0.7554|precision=0.7202|recall=0.7943|f1=0.7554|auc=0.7572|aupr=0.8062|pos_acc=0.7943|neg_acc=0.7943\n",
      "-----------------------Fold =  2\n",
      "Train:\n",
      "tn = 738, fp = 0, fn = 0, tp = 732\n",
      "y_pred: 0 = 738 | 1 = 732\n",
      "y_true: 0 = 738 | 1 = 732\n",
      "acc=1.0000|precision=1.0000|recall=1.0000|f1=1.0000|auc=1.0000|aupr=1.0000|pos_acc=1.0000|neg_acc=1.0000\n",
      "Test:\n",
      "tn = 133, fp = 48, fn = 36, tp = 151\n",
      "y_pred: 0 = 169 | 1 = 199\n",
      "y_true: 0 = 181 | 1 = 187\n",
      "acc=0.7717|precision=0.7588|recall=0.8075|f1=0.7824|auc=0.7711|aupr=0.8321|pos_acc=0.8075|neg_acc=0.7870\n",
      "-----------------------Fold =  3\n",
      "Train:\n",
      "tn = 725, fp = 0, fn = 0, tp = 746\n",
      "y_pred: 0 = 725 | 1 = 746\n",
      "y_true: 0 = 725 | 1 = 746\n",
      "acc=1.0000|precision=1.0000|recall=1.0000|f1=1.0000|auc=1.0000|aupr=1.0000|pos_acc=1.0000|neg_acc=1.0000\n",
      "Test:\n",
      "tn = 147, fp = 47, fn = 40, tp = 133\n",
      "y_pred: 0 = 187 | 1 = 180\n",
      "y_true: 0 = 194 | 1 = 173\n",
      "acc=0.7629|precision=0.7389|recall=0.7688|f1=0.7535|auc=0.7633|aupr=0.8083|pos_acc=0.7688|neg_acc=0.7861\n",
      "-----------------------Fold =  4\n",
      "Train:\n",
      "tn = 737, fp = 0, fn = 0, tp = 734\n",
      "y_pred: 0 = 737 | 1 = 734\n",
      "y_true: 0 = 737 | 1 = 734\n",
      "acc=1.0000|precision=1.0000|recall=1.0000|f1=1.0000|auc=1.0000|aupr=1.0000|pos_acc=1.0000|neg_acc=1.0000\n",
      "Test:\n",
      "tn = 132, fp = 50, fn = 47, tp = 138\n",
      "y_pred: 0 = 179 | 1 = 188\n",
      "y_true: 0 = 182 | 1 = 185\n",
      "acc=0.7357|precision=0.7340|recall=0.7459|f1=0.7399|auc=0.7356|aupr=0.8040|pos_acc=0.7459|neg_acc=0.7374\n",
      "========== isbalance = True | task = Tm\n",
      "-------Fold  0\n",
      "# Drug: Train = 148 | Test = 36\n",
      "# Pairs: Train = 1428 | Test = 410\n",
      "-------Fold  1\n",
      "# Drug: Train = 148 | Test = 36\n",
      "# Pairs: Train = 1492 | Test = 346\n",
      "-------Fold  2\n",
      "# Drug: Train = 148 | Test = 36\n",
      "# Pairs: Train = 1547 | Test = 291\n",
      "-------Fold  3\n",
      "# Drug: Train = 148 | Test = 36\n",
      "# Pairs: Train = 1362 | Test = 476\n",
      "-------Fold  4\n",
      "# Drug: Train = 144 | Test = 40\n",
      "# Pairs: Train = 1523 | Test = 315\n",
      "-----------------------Fold =  0\n",
      "Train:\n",
      "tn = 712, fp = 0, fn = 0, tp = 716\n",
      "y_pred: 0 = 712 | 1 = 716\n",
      "y_true: 0 = 712 | 1 = 716\n",
      "acc=1.0000|precision=1.0000|recall=1.0000|f1=1.0000|auc=1.0000|aupr=1.0000|pos_acc=1.0000|neg_acc=1.0000\n",
      "Test:\n",
      "tn = 160, fp = 47, fn = 45, tp = 158\n",
      "y_pred: 0 = 205 | 1 = 205\n",
      "y_true: 0 = 207 | 1 = 203\n",
      "acc=0.7756|precision=0.7707|recall=0.7783|f1=0.7745|auc=0.7756|aupr=0.8294|pos_acc=0.7783|neg_acc=0.7805\n",
      "-----------------------Fold =  1\n",
      "Train:\n",
      "tn = 747, fp = 0, fn = 0, tp = 745\n",
      "y_pred: 0 = 747 | 1 = 745\n",
      "y_true: 0 = 747 | 1 = 745\n",
      "acc=1.0000|precision=1.0000|recall=1.0000|f1=1.0000|auc=1.0000|aupr=1.0000|pos_acc=1.0000|neg_acc=1.0000\n",
      "Test:\n",
      "tn = 125, fp = 47, fn = 51, tp = 123\n",
      "y_pred: 0 = 176 | 1 = 170\n",
      "y_true: 0 = 172 | 1 = 174\n",
      "acc=0.7168|precision=0.7235|recall=0.7069|f1=0.7151|auc=0.7168|aupr=0.7889|pos_acc=0.7069|neg_acc=0.7102\n",
      "-----------------------Fold =  2\n",
      "Train:\n",
      "tn = 760, fp = 0, fn = 0, tp = 787\n",
      "y_pred: 0 = 760 | 1 = 787\n",
      "y_true: 0 = 760 | 1 = 787\n",
      "acc=1.0000|precision=1.0000|recall=1.0000|f1=1.0000|auc=1.0000|aupr=1.0000|pos_acc=1.0000|neg_acc=1.0000\n",
      "Test:\n",
      "tn = 115, fp = 44, fn = 43, tp = 89\n",
      "y_pred: 0 = 158 | 1 = 133\n",
      "y_true: 0 = 159 | 1 = 132\n",
      "acc=0.7010|precision=0.6692|recall=0.6742|f1=0.6717|auc=0.6988|aupr=0.7456|pos_acc=0.6742|neg_acc=0.7278\n",
      "-----------------------Fold =  3\n",
      "Train:\n",
      "tn = 715, fp = 0, fn = 0, tp = 647\n",
      "y_pred: 0 = 715 | 1 = 647\n",
      "y_true: 0 = 715 | 1 = 647\n",
      "acc=1.0000|precision=1.0000|recall=1.0000|f1=1.0000|auc=1.0000|aupr=1.0000|pos_acc=1.0000|neg_acc=1.0000\n",
      "Test:\n",
      "tn = 146, fp = 58, fn = 94, tp = 178\n",
      "y_pred: 0 = 240 | 1 = 236\n",
      "y_true: 0 = 204 | 1 = 272\n",
      "acc=0.6807|precision=0.7542|recall=0.6544|f1=0.7008|auc=0.6850|aupr=0.8031|pos_acc=0.6544|neg_acc=0.6083\n",
      "-----------------------Fold =  4\n",
      "Train:\n",
      "tn = 742, fp = 0, fn = 0, tp = 781\n",
      "y_pred: 0 = 742 | 1 = 781\n",
      "y_true: 0 = 742 | 1 = 781\n",
      "acc=1.0000|precision=1.0000|recall=1.0000|f1=1.0000|auc=1.0000|aupr=1.0000|pos_acc=1.0000|neg_acc=1.0000\n",
      "Test:\n",
      "tn = 126, fp = 51, fn = 34, tp = 104\n",
      "y_pred: 0 = 160 | 1 = 155\n",
      "y_true: 0 = 177 | 1 = 138\n",
      "acc=0.7302|precision=0.6710|recall=0.7536|f1=0.7099|auc=0.7327|aupr=0.7663|pos_acc=0.7536|neg_acc=0.7875\n",
      "========== isbalance = True | task = Td\n",
      "-------Fold  0\n",
      "# Mutation: Train = 483 | Test = 120\n",
      "# Pairs: Train = 1470 | Test = 368\n",
      "-------Fold  1\n",
      "# Mutation: Train = 483 | Test = 120\n",
      "# Pairs: Train = 1467 | Test = 371\n",
      "-------Fold  2\n",
      "# Mutation: Train = 483 | Test = 120\n",
      "# Pairs: Train = 1467 | Test = 371\n",
      "-------Fold  3\n",
      "# Mutation: Train = 483 | Test = 120\n",
      "# Pairs: Train = 1505 | Test = 333\n",
      "-------Fold  4\n",
      "# Mutation: Train = 480 | Test = 123\n",
      "# Pairs: Train = 1443 | Test = 395\n",
      "-----------------------Fold =  0\n",
      "Train:\n",
      "tn = 747, fp = 0, fn = 0, tp = 723\n",
      "y_pred: 0 = 747 | 1 = 723\n",
      "y_true: 0 = 747 | 1 = 723\n",
      "acc=1.0000|precision=1.0000|recall=1.0000|f1=1.0000|auc=1.0000|aupr=1.0000|pos_acc=1.0000|neg_acc=1.0000\n",
      "Test:\n",
      "tn = 130, fp = 42, fn = 54, tp = 142\n",
      "y_pred: 0 = 184 | 1 = 184\n",
      "y_true: 0 = 172 | 1 = 196\n",
      "acc=0.7391|precision=0.7717|recall=0.7245|f1=0.7474|auc=0.7402|aupr=0.8215|pos_acc=0.7245|neg_acc=0.7065\n",
      "-----------------------Fold =  1\n",
      "Train:\n",
      "tn = 725, fp = 0, fn = 0, tp = 742\n",
      "y_pred: 0 = 725 | 1 = 742\n",
      "y_true: 0 = 725 | 1 = 742\n",
      "acc=1.0000|precision=1.0000|recall=1.0000|f1=1.0000|auc=1.0000|aupr=1.0000|pos_acc=1.0000|neg_acc=1.0000\n",
      "Test:\n",
      "tn = 152, fp = 42, fn = 35, tp = 142\n",
      "y_pred: 0 = 187 | 1 = 184\n",
      "y_true: 0 = 194 | 1 = 177\n",
      "acc=0.7925|precision=0.7717|recall=0.8023|f1=0.7867|auc=0.7929|aupr=0.8342|pos_acc=0.8023|neg_acc=0.8128\n",
      "-----------------------Fold =  2\n",
      "Train:\n",
      "tn = 722, fp = 0, fn = 0, tp = 745\n",
      "y_pred: 0 = 722 | 1 = 745\n",
      "y_true: 0 = 722 | 1 = 745\n",
      "acc=1.0000|precision=1.0000|recall=1.0000|f1=1.0000|auc=1.0000|aupr=1.0000|pos_acc=1.0000|neg_acc=1.0000\n",
      "Test:\n",
      "tn = 153, fp = 44, fn = 48, tp = 126\n",
      "y_pred: 0 = 201 | 1 = 170\n",
      "y_true: 0 = 197 | 1 = 174\n",
      "acc=0.7520|precision=0.7412|recall=0.7241|f1=0.7326|auc=0.7504|aupr=0.7973|pos_acc=0.7241|neg_acc=0.7612\n",
      "-----------------------Fold =  3\n",
      "Train:\n",
      "tn = 755, fp = 0, fn = 0, tp = 750\n",
      "y_pred: 0 = 755 | 1 = 750\n",
      "y_true: 0 = 755 | 1 = 750\n",
      "acc=1.0000|precision=1.0000|recall=1.0000|f1=1.0000|auc=1.0000|aupr=1.0000|pos_acc=1.0000|neg_acc=1.0000\n",
      "Test:\n",
      "tn = 115, fp = 49, fn = 30, tp = 139\n",
      "y_pred: 0 = 145 | 1 = 188\n",
      "y_true: 0 = 164 | 1 = 169\n",
      "acc=0.7628|precision=0.7394|recall=0.8225|f1=0.7787|auc=0.7619|aupr=0.8260|pos_acc=0.8225|neg_acc=0.7931\n",
      "-----------------------Fold =  4\n",
      "Train:\n",
      "tn = 727, fp = 0, fn = 0, tp = 716\n",
      "y_pred: 0 = 727 | 1 = 716\n",
      "y_true: 0 = 727 | 1 = 716\n",
      "acc=1.0000|precision=1.0000|recall=1.0000|f1=1.0000|auc=1.0000|aupr=1.0000|pos_acc=1.0000|neg_acc=1.0000\n",
      "Test:\n",
      "tn = 143, fp = 49, fn = 51, tp = 152\n",
      "y_pred: 0 = 194 | 1 = 201\n",
      "y_true: 0 = 192 | 1 = 203\n",
      "acc=0.7468|precision=0.7562|recall=0.7488|f1=0.7525|auc=0.7468|aupr=0.8171|pos_acc=0.7488|neg_acc=0.7371\n"
     ]
    }
   ],
   "source": [
    "directory='C:/Users/Administrator/Desktop/图采样data/last data'\n",
    "#directory = 'data'\n",
    "for isbalance in [True]:\n",
    "#for isbalance in [True, False]:\n",
    "    \n",
    "    ID, IM, dtp, mirna_ids, disease_ids, mirna_test_num, disease_test_num, samples = obtain_data(directory, \n",
    "                                                                                                 isbalance)\n",
    "    for task in ['Tp', 'Tm', 'Td']:\n",
    "        \n",
    "        print('========== isbalance = {} | task = {}'.format(isbalance, task))\n",
    "        \n",
    "        if task == 'Tp':\n",
    "            train_index_all, test_index_all, train_id_all, test_id_all = generate_task_Tp_train_test_idx(samples)\n",
    "            \n",
    "        elif task == 'Tm':\n",
    "            item = 'Drug'\n",
    "            ids = mirna_ids\n",
    "            train_index_all, test_index_all, train_id_all, test_id_all = generate_task_Tm_Td_train_test_idx(item, ids, dtp)\n",
    "\n",
    "        elif task == 'Td':\n",
    "            item = 'Mutation'\n",
    "            ids = disease_ids\n",
    "            train_index_all, test_index_all, train_id_all, test_id_all = generate_task_Tm_Td_train_test_idx(item, ids, dtp)\n",
    "            \n",
    "        ys_train, metrics_train, ys_test, metrics_test = run_rf(train_index_all, test_index_all, samples, 'DT')"
   ]
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
